package com.itheima.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

public final class Utils {

//    1、私有化构造方法

    private Utils() {
    }

//    2、将需要的成员变量数据静态私有化

    private static SqlSession sqlSession = null;

//    3、利用静态代码块初始化静态私有成员变量

    static {
        try {
            InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
            SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);
            sqlSession = build.openSession(true);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

//    4、利用方法获取私有成员变量


    public static SqlSession getSqlSession() {
        return sqlSession;
    }
}
